import {
  AbilityType,
  APIType,
  APIVersion,
  LanguageType,
  ModuleTemplateType,
  RuntimeOSType
} from '../../../../../models/constants';
import { ModuleTemplateOption, ModuleTemplateOptionBuilder } from '../../../../../models/model-options';
import { ModuleConfigInitFun } from '../../../../../models/module-models';
import { copyTmp, DirTemplateCreator, dirTmp, renderTmp } from '../../../../../models/template-models';
import { entryBuilder, moduleBuilder } from '../../../../../module-builder';

/**
 * Entry
 *
 * @type {ModuleTemplateOption}
 */
const emptyAbilityEntryOption: ModuleTemplateOption = ModuleTemplateOptionBuilder.builder()
  .abilityType(AbilityType.AtomicService)
  .apiType(APIType.STAGE)
  .apiVersion(APIVersion.API11)
  .language(LanguageType.ArkTs)
  .moduleTemplateType(ModuleTemplateType.EmptyAbility)
  .runtimeOS(RuntimeOSType.HarmonyOS)
  .build();

const emptyAbilityEntryTemplate: DirTemplateCreator = (dirName) => dirTmp(dirName)
  .add(dirTmp('src')
    .add(dirTmp('main')
      .add(dirTmp('ets')
        .add(copyTmp('/entry/stage/service/emptyAbility/api11/src/main/ets/entryability'))
        .add(copyTmp('/entry/stage/service/emptyAbility/api11/src/main/ets/pages'))
        .add(copyTmp('/entry/stage/service/emptyAbility/api11/src/main/ets/entryformability'))
        .add(dirTmp('widget')
          .add(copyTmp('/entry/stage/service/emptyAbility/api11/src/main/ets/widget/pages'))
        )
      )
      .add(dirTmp('resources')
        .add(dirTmp('base')
          .add(dirTmp('element')
            .add(copyTmp('/entry/stage/service/emptyAbility/api11/src/main/resources/base/element/color.json'))
            .add(copyTmp('/entry/stage/service/emptyAbility/api11/src/main/resources/base/element/string.json'))
            .add(copyTmp('/entry/stage/service/emptyAbility/api11/src/main/resources/base/element/float.json'))
          )
          .add(copyTmp('/entry/stage/service/emptyAbility/api11/src/main/resources/base/media'))
          .add(copyTmp('/entry/stage/service/emptyAbility/api11/src/main/resources/base/profile'))
        )
        .add(copyTmp('/entry/stage/service/emptyAbility/api11/src/main/resources/en_US'))
        .add(dirTmp('rawfile'))
        .add(copyTmp('/entry/stage/service/emptyAbility/api11/src/main/resources/zh_CN'))
      )
      .add(renderTmp('/entry/stage/service/emptyAbility/api11/src/main/module.json5'))
    )
    .add(dirTmp('ohosTest')
      .add(copyTmp('/entry/stage/service/emptyAbility/api11/src/ohosTest/ets'))
      .add(copyTmp('/entry/stage/service/emptyAbility/api11/src/ohosTest/resources'))
      .add(renderTmp('/entry/stage/service/emptyAbility/api11/src/ohosTest/module.json5'))
    )
    .add(copyTmp('/entry/stage/service/emptyAbility/api11/src/test'))
  )
  .add(renderTmp('/module/stage/service/emptyAbility/api11/build-profile.json5'))
  .add(copyTmp('/module/stage/service/common/api10/hvigorfile.ts'))
  .add(copyTmp('/module/stage/service/common/api10/obfuscation-rules.txt'))
  .add(renderTmp('/module/stage/service/common/api10/oh-package.json5'));


const initFun: ModuleConfigInitFun = async (name, option, template) => {
  template.node('src').node('main').node('module.json5').data = { moduleName: name };
  template.node('src').node('ohosTest').node('module.json5').data = { moduleName: name };
  template.node('build-profile.json5').data = { apiType: option.apiType, runtimeOS: option.runtimeOS };
  template.node('oh-package.json5').data = { moduleName: name };
};

entryBuilder.add(emptyAbilityEntryOption, emptyAbilityEntryTemplate, initFun);

/**
 * EmptyAbility
 *
 * @type {ModuleTemplateOption}
 */
const emptyAbilityOption: ModuleTemplateOption = ModuleTemplateOptionBuilder.builder()
  .abilityType(AbilityType.AtomicService)
  .apiType(APIType.STAGE)
  .apiVersion(APIVersion.API11)
  .language(LanguageType.ArkTs)
  .moduleTemplateType(ModuleTemplateType.EmptyAbility)
  .runtimeOS(RuntimeOSType.HarmonyOS)
  .build();

const emptyAbilityTemplate: DirTemplateCreator = (dirName) => dirTmp(dirName)
  .add(dirTmp('src')
    .add(dirTmp('main')
      .add(dirTmp('ets')
        .add(copyTmp('/module/stage/service/application/api11/src/main/ets/applicationability'))
        .add(copyTmp('/module/stage/service/application/api11/src/main/ets/applicationformability'))
        .add(copyTmp('/module/stage/service/application/api11/src/main/ets/pages'))
        .add(copyTmp('/module/stage/service/application/api11/src/main/ets/widget'))
      )
      .add(dirTmp('resources')
        .add(dirTmp('base')
          .add(dirTmp('element')
            .add(copyTmp('/module/stage/service/application/api11/src/main/resources/base/element/color.json'))
            .add(copyTmp('/module/stage/service/application/api11/src/main/resources/base/element/string.json'))
            .add(copyTmp('/module/stage/service/application/api11/src/main/resources/base/element/float.json'))
          )
          .add(copyTmp('/module/stage/service/application/api11/src/main/resources/base/media'))
          .add(copyTmp('/module/stage/service/application/api11/src/main/resources/base/profile'))
        )
        .add(copyTmp('/module/stage/service/application/api11/src/main/resources/en_US'))
        .add(dirTmp('rawfile'))
        .add(copyTmp('/module/stage/service/application/api11/src/main/resources/zh_CN'))
      )
      .add(renderTmp('/module/stage/service/application/api11/src/main/module.json5'))
    )
    .add(dirTmp('ohosTest')
      .add(copyTmp('/module/stage/service/application/api11/src/ohosTest/ets'))
      .add(copyTmp('/module/stage/service/application/api11/src/ohosTest/resources'))
      .add(renderTmp('/module/stage/service/application/api11/src/ohosTest/module.json5'))
    )
    .add(copyTmp('/module/stage/service/application/api11/src/test'))
  )
  .add(renderTmp('/module/stage/service/application/api11/build-profile.json5'))
  .add(copyTmp('/module/stage/service/common/api10/hvigorfile.ts'))
  .add(copyTmp('/module/stage/service/common/api10/obfuscation-rules.txt'))
  .add(renderTmp('/module/stage/service/common/api10/oh-package.json5'));

moduleBuilder.add(emptyAbilityOption, emptyAbilityTemplate, initFun);

/**
 * SharedLibrary
 *
 * @type {ModuleTemplateOption}
 */
const shareLibraryOption: ModuleTemplateOption = ModuleTemplateOptionBuilder.builder()
  .abilityType(AbilityType.AtomicService)
  .apiType(APIType.STAGE)
  .apiVersion(APIVersion.API11)
  .language(LanguageType.ArkTs)
  .moduleTemplateType(ModuleTemplateType.SharedLibrary)
  .runtimeOS(RuntimeOSType.HarmonyOS)
  .build();

const shareLibraryTemplate: DirTemplateCreator = (dirName) => dirTmp(dirName)
  .add(dirTmp('src')
    .add(dirTmp('main')
      .add(dirTmp('ets')
        .add(copyTmp('/module/stage/service/sharedLibrary/api11/src/main/ets/pages'))
      )
      .add(dirTmp('resources')
        .add(dirTmp('base')
          .add(copyTmp('/module/stage/service/sharedLibrary/api11/src/main/resources/base/element'))
          .add(copyTmp('/module/stage/service/sharedLibrary/api11/src/main/resources/base/media'))
          .add(copyTmp('/module/stage/service/sharedLibrary/api11/src/main/resources/base/profile'))
        )
        .add(dirTmp('rawfile'))
      )
      .add(renderTmp('/module/stage/service/sharedLibrary/api11/src/main/module.json5'))
    )
    .add(copyTmp('/module/stage/service/common/api11/src/test'))
  )
  .add(renderTmp('/module/stage/service/sharedLibrary/api11/build-profile.json5'))
  .add(copyTmp('/module/stage/service/sharedLibrary/api11/hvigorfile.ts'))
  .add(copyTmp('/module/stage/service/common/api11/obfuscation-rules.txt'))
  .add(renderTmp('/module/stage/service/sharedLibrary/api11/oh-package.json5'));

moduleBuilder.add(shareLibraryOption, shareLibraryTemplate, initFun);


/**
 * StaticLibrary
 *
 * @type {ModuleTemplateOption}
 */
const staticLibraryOption: ModuleTemplateOption = ModuleTemplateOptionBuilder.builder()
  .abilityType(AbilityType.AtomicService)
  .apiType(APIType.STAGE)
  .apiVersion(APIVersion.API11)
  .language(LanguageType.ArkTs)
  .moduleTemplateType(ModuleTemplateType.StaticLibrary)
  .runtimeOS(RuntimeOSType.HarmonyOS)
  .build();

const staticLibraryTemplate: DirTemplateCreator = (dirName) => dirTmp(dirName)
  .add(dirTmp('src')
    .add(dirTmp('main')
      .add(copyTmp('/module/stage/service/staticLibrary/api11/src/main/ets'))
      .add(copyTmp('/module/stage/service/staticLibrary/api11/src/main/resources'))
      .add(renderTmp('/module/stage/service/staticLibrary/api11/src/main/module.json5'))
    )
    .add(copyTmp('/module/stage/service/common/api11/src/test'))
  )
  .add(renderTmp('/module/stage/service/staticLibrary/api11/build-profile.json5'))
  .add(copyTmp('/module/stage/service/common/api11/consumer-rules.txt'))
  .add(copyTmp('/module/stage/service/staticLibrary/api11/hvigorfile.ts'))
  .add(copyTmp('/module/stage/service/staticLibrary/api11/Index.ets'))
  .add(copyTmp('/module/stage/service/common/api11/obfuscation-rules.txt'))
  .add(renderTmp('/module/stage/service/staticLibrary/api11/oh-package.json5'));

moduleBuilder.add(staticLibraryOption, staticLibraryTemplate, initFun);

const serviceNativeApplicationAbilityOption: ModuleTemplateOption = ModuleTemplateOptionBuilder.builder()
  .abilityType(AbilityType.AtomicService)
  .apiType(APIType.STAGE)
  .apiVersion(APIVersion.API11)
  .language(LanguageType.ArkTs)
  .moduleTemplateType(ModuleTemplateType.NativeC)
  .runtimeOS(RuntimeOSType.HarmonyOS)
  .build();

const serviceNativeApplicationAbilityTemplate: DirTemplateCreator = (dirName) => dirTmp(dirName)
  .add(dirTmp('src')
    .add(dirTmp('main')
      .add(dirTmp('cpp')
        .add(dirTmp('types'))
        .add(dirTmp('libapplication'))
        .add(copyTmp('/module/stage/service/nativeApplication/api11/src/main/cpp/types/libapplication/index.d.ts'))
        .add(renderTmp('/module/stage/service/nativeApplication/api11/src/main/cpp/types/libapplication/oh-package.json5'))
        .add(copyTmp('/module/stage/service/nativeApplication/api11/src/main/cpp/CMakeLists.txt'))
        .add(copyTmp('/module/stage/service/nativeApplication/api11/src/main/cpp/hello.cpp'))
      )
      .add(dirTmp('ets')
        .add(copyTmp('/module/stage/service/nativeApplication/api11/src/main/ets/applicationability'))
        .add(copyTmp('/module/stage/service/nativeApplication/api11/src/main/ets/applicationformability'))
        .add(copyTmp('/module/stage/service/nativeApplication/api11/src/main/ets/pages'))
        .add(copyTmp('/module/stage/service/nativeApplication/api11/src/main/ets/widget'))
      )
      .add(dirTmp('resources')
        .add(dirTmp('base')
          .add(dirTmp('element')
            .add(copyTmp('/module/stage/service/nativeApplication/api11/src/main/resources/base/element/color.json'))
            .add(copyTmp('/module/stage/service/nativeApplication/api11/src/main/resources/base/element/string.json'))
            .add(copyTmp('/module/stage/service/nativeApplication/api11/src/main/resources/base/element/float.json'))
          )
          .add(copyTmp('/module/stage/service/nativeApplication/api11/src/main/resources/base/media'))
          .add(copyTmp('/module/stage/service/nativeApplication/api11/src/main/resources/base/profile'))
        )
        .add(copyTmp('/module/stage/service/nativeApplication/api11/src/main/resources/en_US'))
        .add(dirTmp('rawfile'))
        .add(copyTmp('/module/stage/service/nativeApplication/api11/src/main/resources/zh_CN'))
      )
      .add(renderTmp('/module/stage/service/nativeApplication/api11/src/main/module.json5'))
    )
    .add(dirTmp('ohosTest')
      .add(copyTmp('/module/stage/service/nativeApplication/api11/src/ohosTest/ets'))
      .add(copyTmp('/module/stage/service/nativeApplication/api11/src/ohosTest/resources'))
      .add(renderTmp('/module/stage/service/nativeApplication/api11/src/ohosTest/module.json5'))
    )
    .add(copyTmp('/module/stage/service/nativeApplication/api11/src/test'))
  )
  .add(renderTmp('/module/stage/service/nativeApplication/api11/build-profile.json5'))
  .add(copyTmp('/module/stage/app/common/hvigorfile-hap.ts', 'hvigorfile.ts'))
  .add(copyTmp('/module/stage/app/common/obfuscation-rules.txt'))
  .add(renderTmp('/module/stage/app/common/oh-package.json5'));

moduleBuilder.add(serviceNativeApplicationAbilityOption, serviceNativeApplicationAbilityTemplate, initFun);

const nativeApplicationEntryTemplate: DirTemplateCreator = (dirName) => dirTmp(dirName)
  .add(dirTmp('src')
    .add(dirTmp('main')
      .add(dirTmp('cpp')
        .add(dirTmp('types'))
        .add(dirTmp('libapplication'))
        .add(copyTmp('/entry/stage/service/nativeApplication/api11/src/main/cpp/types/libapplication/index.d.ts'))
        .add(renderTmp('/entry/stage/service/nativeApplication/api11/src/main/cpp/types/libapplication/oh-package.json5'))
        .add(copyTmp('/entry/stage/service/nativeApplication/api11/src/main/cpp/CMakeLists.txt'))
        .add(renderTmp('/entry/stage/service/nativeApplication/api11/src/main/cpp/hello.cpp'))
      )
      .add(dirTmp('ets')
        .add(copyTmp('/entry/stage/service/nativeApplication/api11/src/main/ets/applicationability'))
        .add(copyTmp('/entry/stage/service/nativeApplication/api11/src/main/ets/applicationformability'))
        .add(copyTmp('/entry/stage/service/nativeApplication/api11/src/main/ets/pages'))
        .add(copyTmp('/entry/stage/service/nativeApplication/api11/src/main/ets/widget'))
      )
      .add(dirTmp('resources')
        .add(dirTmp('base')
          .add(dirTmp('element')
            .add(copyTmp('/entry/stage/service/nativeApplication/api11/src/main/resources/base/element/color.json'))
            .add(copyTmp('/entry/stage/service/nativeApplication/api11/src/main/resources/base/element/string.json'))
            .add(copyTmp('/entry/stage/service/nativeApplication/api11/src/main/resources/base/element/float.json'))
          )
          .add(copyTmp('/entry/stage/service/nativeApplication/api11/src/main/resources/base/media'))
          .add(copyTmp('/entry/stage/service/nativeApplication/api11/src/main/resources/base/profile'))
        )
        .add(copyTmp('/entry/stage/service/nativeApplication/api11/src/main/resources/en_US'))
        .add(dirTmp('rawfile'))
        .add(copyTmp('/entry/stage/service/nativeApplication/api11/src/main/resources/zh_CN'))
      )
      .add(renderTmp('/entry/stage/service/nativeApplication/api11/src/main/module.json5'))
    )
    .add(dirTmp('ohosTest')
      .add(copyTmp('/entry/stage/service/nativeApplication/api11/src/ohosTest/ets'))
      .add(copyTmp('/entry/stage/service/nativeApplication/api11/src/ohosTest/resources'))
      .add(renderTmp('/entry/stage/service/nativeApplication/api11/src/ohosTest/module.json5'))
    )
    .add(copyTmp('/entry/stage/service/nativeApplication/api11/src/test'))
  )
  .add(renderTmp('/entry/stage/service/nativeApplication/api11/build-profile.json5'))
  .add(copyTmp('/module/stage/app/common/hvigorfile-hap.ts', 'hvigorfile.ts'))
  .add(copyTmp('/module/stage/app/common/obfuscation-rules.txt'))
  .add(renderTmp('/module/stage/app/common/oh-package.json5'));

const nativeInitFun: ModuleConfigInitFun = async (name, option, template) => {
  // eslint-disable-next-line max-len
  template.node('src').node('main').node('cpp').node('types').node('libapplication').node('oh-package.json5').data = { moduleName: name };
  template.node('src').node('main').node('cpp').node('CMakeLists.txt').data = { moduleName: name };
  template.node('src').node('main').node('module.json5').data = { moduleName: name };
  template.node('src').node('ohosTest').node('module.json5').data = { moduleName: name };
  template.node('build-profile.json5').data = { apiType: option.apiType, runtimeOS: option.runtimeOS };
  template.node('oh-package.json5').data = { moduleName: name };
};

entryBuilder.add(serviceNativeApplicationAbilityOption, nativeApplicationEntryTemplate, nativeInitFun);

const shareNativeCLibraryOption: ModuleTemplateOption = ModuleTemplateOptionBuilder.builder()
  .abilityType(AbilityType.AtomicService)
  .apiType(APIType.STAGE)
  .apiVersion(APIVersion.API11)
  .language(LanguageType.ArkTs)
  .moduleTemplateType(ModuleTemplateType.SharedLibraryWithC)
  .runtimeOS(RuntimeOSType.HarmonyOS)
  .build();

const shareNativeCLibraryTemplate: DirTemplateCreator = (dirName) => dirTmp(dirName)
  .add(dirTmp('src')
    .add(dirTmp('main')
      .add(dirTmp('cpp')
        .add(dirTmp('types'))
        .add(dirTmp('liblibrary1'))
        .add(copyTmp('/module/stage/service/shareNativeCLibrary/api11/src/main/cpp/types/liblibrary1/index.d.ts'))
        .add(renderTmp('/module/stage/service/shareNativeCLibrary/api11/src/main/cpp/types/liblibrary1/oh-package.json5'))
        .add(copyTmp('/module/stage/service/shareNativeCLibrary/api11/src/main/cpp/CMakeLists.txt'))
        .add(copyTmp('/module/stage/service/shareNativeCLibrary/api11/src/main/cpp/hello.cpp'))
      )
      .add(dirTmp('ets')
        .add(copyTmp('/module/stage/service/shareNativeCLibrary/api11/src/main/ets/pages'))
      )
      .add(dirTmp('resources')
        .add(dirTmp('base')
          .add(dirTmp('element')
            .add(copyTmp('/module/stage/service/shareNativeCLibrary/api11/src/main/resources/base/element/color.json'))
            .add(copyTmp('/module/stage/service/shareNativeCLibrary/api11/src/main/resources/base/element/string.json'))
          )
          .add(copyTmp('/module/stage/service/shareNativeCLibrary/api11/src/main/resources/base/media'))
          .add(copyTmp('/module/stage/service/shareNativeCLibrary/api11/src/main/resources/base/profile'))
        )
        .add(dirTmp('rawfile'))
      )
      .add(renderTmp('/module/stage/service/shareNativeCLibrary/api11/src/main/module.json5'))
    )
    .add(copyTmp('/module/stage/service/shareNativeCLibrary/api11/src/test'))
  )
  .add(renderTmp('/module/stage/service/shareNativeCLibrary/api11/build-profile.json5'))
  .add(copyTmp('/module/stage/service/shareNativeCLibrary/api11/hvigorfile-hap.ts', 'hvigorfile.ts'))
  .add(copyTmp('/module/stage/service/shareNativeCLibrary/api11/obfuscation-rules.txt'))
  .add(renderTmp('/module/stage/service/shareNativeCLibrary/api11/oh-package.json5'));

moduleBuilder.add(shareNativeCLibraryOption, shareNativeCLibraryTemplate, initFun);

const staticNativeCLibraryOption: ModuleTemplateOption = ModuleTemplateOptionBuilder.builder()
  .abilityType(AbilityType.AtomicService)
  .apiType(APIType.STAGE)
  .apiVersion(APIVersion.API11)
  .language(LanguageType.ArkTs)
  .moduleTemplateType(ModuleTemplateType.StaticLibraryWithC)
  .runtimeOS(RuntimeOSType.HarmonyOS)
  .build();

const staticNativeCLibraryTemplate: DirTemplateCreator = (dirName) => dirTmp(dirName)
  .add(dirTmp('src')
    .add(dirTmp('main')
      .add(dirTmp('cpp')
        .add(dirTmp('types'))
        .add(dirTmp('liblibrary2'))
        .add(copyTmp('/module/stage/service/staticNativeCLibrary/api11/src/main/cpp/types/liblibrary2/index.d.ts'))
        .add(renderTmp('/module/stage/service/staticNativeCLibrary/api11/src/main/cpp/types/liblibrary2/oh-package.json5'))
        .add(copyTmp('/module/stage/service/staticNativeCLibrary/api11/src/main/cpp/CMakeLists.txt'))
        .add(copyTmp('/module/stage/service/staticNativeCLibrary/api11/src/main/cpp/hello.cpp'))
      )
      .add(dirTmp('ets')
        .add(copyTmp('/module/stage/service/staticNativeCLibrary/api11/src/main/ets/components/mainpage'))
      )
      .add(dirTmp('resources')
        .add(dirTmp('base')
          .add(dirTmp('element')
            .add(copyTmp('/module/stage/service/staticNativeCLibrary/api11/src/main/resources/base/element/string.json'))
          )
        )
        .add(copyTmp('/module/stage/service/staticNativeCLibrary/api11/src/main/resources/en_US'))
        .add(copyTmp('/module/stage/service/staticNativeCLibrary/api11/src/main/resources/zh_CN'))
      )
      .add(renderTmp('/module/stage/service/staticNativeCLibrary/api11/src/main/module.json5'))
    )
    .add(copyTmp('/module/stage/service/staticNativeCLibrary/api11/src/test'))
  )
  .add(renderTmp('/module/stage/service/staticNativeCLibrary/api11/build-profile.json5'))
  .add(copyTmp('/module/stage/service/staticNativeCLibrary/api11/hvigorfile-hap.ts', 'hvigorfile.ts'))
  .add(copyTmp('/module/stage/service/staticNativeCLibrary/api11/obfuscation-rules.txt'))
  .add(renderTmp('/module/stage/service/staticNativeCLibrary/api11/oh-package.json5'));

moduleBuilder.add(staticNativeCLibraryOption, staticNativeCLibraryTemplate, initFun);